package com.banmajio.mapper;

import com.banmajio.pojo.VolunteerTrajectory;
import com.banmajio.pojo.YoungsterInfo;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@DS("master")
@Mapper
public interface YoungsterInfoMapper extends BaseMapper<YoungsterInfo> {


    @Select("SELECT nextval(#{sequenceName})")
    Long getNextSequenceValue(@Param("sequenceName") String sequenceName);

    /**
     * 批量插入数据
     *
     * @param youngsterInfoList 要插入的数据列表
     * @param fieldNames        要插入的字段名列表
     * @return 插入的行数
     */
//    @Select("INSERT INTO YoungsterInfo (${fieldNames}) VALUES (${youngsterInfo})")
    @Insert("<script>" +
            "INSERT INTO ${targetTable} " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "<foreach collection='fieldNames' item='field' separator=','> " +
            "${field} " +  // 动态生成字段列表
            "</foreach> " +
            "</trim> " +
            "VALUES " +
            "<foreach collection='youngsterInfoList' item='item' separator=','> " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "<foreach collection='fieldNames' item='field' separator=','> " +
            "#{item.${field}} " +  // 动态生成值列表
            "</foreach> " +
            "</trim> " +
            "</foreach>" +
            "</script>")
    Boolean batchInsertYoung(@Param("youngsterInfoList") List<YoungsterInfo> youngsterInfoList,@Param("targetTable") String targetTable,@Param("fieldNames") List<String> fieldNames);

    @Insert("<script>" +
            "INSERT INTO ${targetTable} " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "<foreach collection='fieldNames' item='field' separator=','> " +
            "${field} " +  // 动态生成字段列表
            "</foreach> " +
            "</trim> " +
            "VALUES " +
            "<foreach collection='trajectoryInfoList' item='item' separator=','> " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "<foreach collection='fieldNames' item='field' separator=','> " +
            "#{item.${field}} " +  // 动态生成值列表
            "</foreach> " +
            "</trim> " +
            "</foreach>" +
            "</script>")
    boolean batchInsertTrajectory(@Param("trajectoryInfoList") List<VolunteerTrajectory> trajectoryInfoList,@Param("targetTable") String targetTable,@Param("fieldNames") List<String> fieldNames);
}
